package com.cisetech.mapper.xml;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

//import org.apache.commons.lang.StringUtils;


import java.io.StringWriter;
public class JavaToXml {
	public String javaToXml(Object object){
			try {
				JAXBContext context=JAXBContext.newInstance(object.getClass());//jdk1.5以后就自带这个包了要是没有的话可以导入
				Marshaller marshaller = context.createMarshaller();//接收对象
				marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);//加上这个以后表头就不起左右了
				marshaller.setProperty(Marshaller.JAXB_ENCODING,"utf-8");//设置xml头部编码格式
				StringWriter writer = new StringWriter(); 
				marshaller.marshal(object,writer);//将对象给StringWriter对象
				String x = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
				String aa = (x+writer.toString()).replaceAll("="+"\"\\d"+"\"", "");
				String d="\\d";
				for(int i=0;i<18;i++){
					d="\\d"+d;
					aa = aa.replaceAll("="+"\""+d+"\"", "");
				}
				return aa;
			} catch (JAXBException e) {
				e.printStackTrace();
			}
			return null;
	}
}
